$("input[type='radio']").click(function(){
    var val_r = $(this).val();
    $(".box_c_a").addClass("box_c_not");
    $(".box_c_a").removeClass("box_c_a");
    $(this).siblings().addClass("box_c_a");
    $(this).siblings().removeClass("box_c_not");
});
var allVariate="box_e",allCount=0,psdData ,container;
// var userid = $("${userid}");
var userid = $("#userId").val();
var apiUri =$("#apiUri").val();
// // var userid = "74bc2cec5ead45db942d0163b94170ca";
$(".box_d_1").click(function (){
//     $.get(apiUri+'api/user/selectUserPayPasswordState'+"?userId="+userid, function(result){
//         alert(result);
//     });
    // query you password
    $.ajax({
        url:apiUri+'api/user/selectUserPayPasswordState',
        data:{userId:userid,dt:Math.random()},
        type: 'get',
        success:function (data) {
            if(data.erroCode == 1){
                $(".box_f").show();
                allVariate = "box_f";
                container = document.getElementById("inputBoxSet");
            }else{
                allVariate = "box_e";
                container = document.getElementById("inputBoxContainer");
                $(".box_e").show();
            }
            boxInput = {
                maxLength:"",
                realInput:"",
                bogusInput:"",
                bogusInputArr:"",
                callback:"",
                init:function(fun){
                    var that = this;
                    this.callback = fun;
                    that.realInput = container.children[0];
                    that.bogusInput = container.children[1];
                    that.bogusInputArr = that.bogusInput.children;
                    that.maxLength = that.bogusInputArr[0].getAttribute("maxlength");
                    that.realInput.oninput = function(){
                        that.setValue();
                    }
                    that.realInput.onpropertychange = function(){
                        that.setValue();
                    }
                },
                setValue:function(){
                    this.realInput.value = this.realInput.value.replace(/\D/g,"");
                    var real_str = this.realInput.value;
                    for(var i = 0 ; i < this.maxLength ; i++){
                        this.bogusInputArr[i].value = real_str[i]?real_str[i]:"";
                    }
                    if(real_str.length == this.maxLength){
                        this.realInput.value = real_str.substring(0,6);
                        this.callback();
                    }
                }, getBoxInputValue:function(){
                    var realValue = "";
                    for(var i in this.bogusInputArr){
                        if(!this.bogusInputArr[i].value){
                            break;
                        }
                        if(parseInt(0)<= i &&  i <= parseInt(6)){
                            realValue += this.bogusInputArr[i].value;
                        }
                    }
                    return realValue;
                }
            }
            boxInput.init(function(){
                getValue();
            });
        }

    });

});
$(".close").click(function (){
    $(".box_e").hide();
    $(".box_f").hide();
    $(".box_e_5,#one,#two,#three,#four,#five,#sex").val("");
    $(".box_f_5,#one1,#two1,#three1,#four1,#five1,#sex1").val("");
});

function getValue(){ // 交易密码输入完成自动执行
    if(allVariate == "box_f"){
        if(allCount ==0){
            console.log("请在此输入密码" ,allCount);
            psdData = boxInput.getBoxInputValue();
            $(".box_f_font").text("请再次输入交易密码");
            $(".box_f_5,#one1,#two1,#three1,#four1,#five1,#sex1").val("");
            allCount++;
        }else{
            var twoData = boxInput.getBoxInputValue();
            if(psdData != twoData){  //两次输入的交易密码不同
                alert_firm("两次输入的交易密码不同");
                $(".box_f_5,#one1,#two1,#three1,#four1,#five1,#sex1").val("");
                return false;
            }else{
                $.ajax({
                    url:apiUri+'api/user/saveUserPayPassword',
                    data:{userId: userid,userPayPassword:psdData},
                    type:"get",
                    success:function (data) {
                        if(data.status == "1"){
                            alert_firm("设置成功!");
                            allCount =0;
                            allVariate="";
                            $(".box_f").hide();
                            $(".box_f_5,#one1,#two1,#three1,#four1,#five1,#sex1").val("");
                        }else{
                            alert_firm("设置失败，请重新输入");
                        }
                    }
                });
            }
        }
    }else{
        var password =boxInput.getBoxInputValue();
        $.post("doPay?a="+Math.random(),{payPassword:password}, function(data) {
            if(data.status==="0000"){
                window.location.href="payDetail";
            }else{
                var msg="";
                if(data.status==="0001"){
                    msg=data.data;
                }
                if(data.status==="0002"){
                    msg=data.data.msg;
                }
                if(data.status==="0003"){
                    msg=data.data;
                }
                if(data.status==="0004"){
                    msg=data.data;
                }
                window.location.href="payError?msg="+msg;
            }
        });
    }
}
function alert_firm(text){
    var  newElement = $('<div class="alert_firm" style="position:fixed;text-align:center;width:70%; left: 15%;background: rgba(0, 0, 0, 0.6); color: #fff; height: 1rem ;line-height:1rem;border-radius: .2rem; top: 45%;"> <span class="al_font" style="padding: .2rem;">'+text+'</span> </div>');
    $("body").append(newElement);
    setTimeout(function () {
        $(".alert_firm").fadeOut("slow");
        $(".alert_firm").fadeOut(1000);
        $(".alert_firm").remove();
    }, 1000)
}